package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldMaterialAllocateDetails;
import com.cleaningcloud.cld.domain.CldMaterialAppDetails;
import com.cleaningcloud.cld.domain.dto.CldMaterialAllocateDto;
import com.cleaningcloud.cld.domain.dto.CldMaterialAppDto;
import com.cleaningcloud.cld.domain.vo.CldMaterialAllocateDetailsVo;
import com.cleaningcloud.cld.domain.vo.CldMaterialAppDetailsVo;
import com.cleaningcloud.cld.domain.vo.CldMaterialAppVo;
import com.cleaningcloud.cld.mapper.CldMaterialAppMapper;
import com.cleaningcloud.cld.service.ICldMaterialAllocateService;
import com.cleaningcloud.cld.service.ICldMaterialAppService;
import com.cleaningcloud.common.core.constant.SecurityConstants;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.UniqueIdUtils;
import com.cleaningcloud.common.security.utils.CommunityTypeConversionUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.api.RemoteWareService;
import com.cleaningcloud.erp.api.domain.ErpWareHouse;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目申请Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-28
 */
@Service
public class CldMaterialAppServiceImpl implements ICldMaterialAppService {
    @Autowired
    private CldMaterialAppMapper cldMaterialAppMapper;

    @Autowired
    private ICldMaterialAllocateService materialAllocateService;

    @Autowired
    private RemoteWareService remoteWareService;

    /**
     * 查询项目申请
     *
     * @param materialAppId 项目申请主键
     * @return 项目申请
     */
    @Override
    public CldMaterialAppVo selectCldMaterialAppByMaterialAppId(Long materialAppId) {
        CldMaterialAppVo materialAppVo = cldMaterialAppMapper.selectCldMaterialAppByMaterialAppId(materialAppId);
        for (CldMaterialAppDetailsVo materialAppDetailsVo : materialAppVo.getDetailsList()) {
            materialAppDetailsVo.setCommodityType(CommunityTypeConversionUtils.conversion(materialAppDetailsVo.getCommodityType()));
        }
        return materialAppVo;
    }

    /**
     * 查询项目申请列表
     *
     * @param cldMaterialApp 项目申请
     * @return 项目申请
     */
    @Override
    public List<CldMaterialAppVo> selectCldMaterialAppList(CldMaterialAppDto cldMaterialApp) {
        return cldMaterialAppMapper.selectCldMaterialAppList(cldMaterialApp);
    }

    /**
     * 新增项目申请
     *
     * @param cldMaterialApp 项目申请
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldMaterialApp(CldMaterialAppDto cldMaterialApp) {
        if(cldMaterialApp.getDetailsList().isEmpty()){
            throw new RuntimeException("请选择项目申请物资！");
        }
        cldMaterialApp.setAppTime(DateUtils.getNowDate());
        cldMaterialApp.setStaffId(SecurityUtils.getUserId());
        cldMaterialApp.setAppStatus(1L);
        cldMaterialApp.setApproveStatus(0L);
        cldMaterialAppMapper.insertCldMaterialApp(cldMaterialApp);
        if (cldMaterialApp.getDetailsList() != null && !cldMaterialApp.getDetailsList().isEmpty()) {
            cldMaterialApp.getDetailsList().forEach(item -> {
                if(item.getAppNumber() <= 0){
                    throw new RuntimeException("项目申请物资数量必须大于0！");
                }
                item.setMaterialAppId(cldMaterialApp.getMaterialAppId());
            });
            cldMaterialAppMapper.batchInsertMaterialAppDetails(cldMaterialApp.getDetailsList());
        }
        return 1;
    }

    /**
     * 修改项目申请
     *
     * @param cldMaterialApp 项目申请
     * @return 结果
     */
    @Override
    public int updateCldMaterialApp(CldMaterialAppDto cldMaterialApp) {
        return cldMaterialAppMapper.updateCldMaterialApp(cldMaterialApp);
    }

    @Override
    @Transactional
    @GlobalTransactional(rollbackFor = Exception.class)
    public int auditCldMaterialApp(CldMaterialAppDto cldMaterialApp) {

        CldMaterialAppVo materialAppVo = cldMaterialAppMapper.selectCldMaterialAppByMaterialAppId(cldMaterialApp.getMaterialAppId());
        if (materialAppVo == null) {
            throw new RuntimeException("项目申请单据查询失败！");
        }
        materialAppVo.setApproveName(SecurityUtils.getUsername());
        materialAppVo.setApproveTime(DateUtils.getNowDate());
        materialAppVo.setApproveStatus(cldMaterialApp.getApproveStatus());
        materialAppVo.setApproveId(SecurityUtils.getUserId());
        if (materialAppVo.getApproveStatus() == 1) {
            materialAppVo.setAppStatus(2L);
            // 生成物资调拨单
            CldMaterialAllocateDto cldMaterialAllocateDto = new CldMaterialAllocateDto();
            cldMaterialAllocateDto.setMaterialOdd("DB" + UniqueIdUtils.generateUniqueId());
            // 调出仓库默认胜博仓
            cldMaterialAllocateDto.setMaterialOutId(6L);
            // 调入仓库为项目仓
            // 根据项目id查询项目仓信息
            ErpWareHouse erpWareHouse = remoteWareService.getWareHouseByProjectId(materialAppVo.getProjectId(), SecurityConstants.INNER);
            if (erpWareHouse == null) {
                throw new RuntimeException("项目仓查询失败！");
            }
            cldMaterialAllocateDto.setMaterialInId(erpWareHouse.getWareId());
            cldMaterialAllocateDto.setMaterialOutPerson(materialAppVo.getStaffId());
            cldMaterialAllocateDto.setMaterialInPerson(materialAppVo.getStaffId());
            cldMaterialAllocateDto.setMaterialAllocateDate(materialAppVo.getAppTime());
            cldMaterialAllocateDto.setApproveStatus(0L);
            cldMaterialAllocateDto.setAllocateType(2L);
            cldMaterialAllocateDto.setRemark("项目仓物资申请");
            List<CldMaterialAllocateDetails> cldMaterialAllocateDetailsVos = new ArrayList<>();
            for (CldMaterialAppDetails cldMaterialAppDetails : materialAppVo.getDetailsList()) {
                // 物资调拨信息
                CldMaterialAllocateDetails cldMaterialAllocateDetails = new CldMaterialAllocateDetailsVo();
                cldMaterialAllocateDetails.setMaterialAllocateNumber(cldMaterialAppDetails.getAppNumber());
                cldMaterialAllocateDetails.setCommodityId(cldMaterialAppDetails.getCommodityId());
                cldMaterialAllocateDetailsVos.add(cldMaterialAllocateDetails);
            }
            cldMaterialAllocateDto.setDetailsList(cldMaterialAllocateDetailsVos);
            // 插入物资调拨信息
            cldMaterialAllocateDto.setIsConfirm(1L);
            materialAllocateService.insertCldMaterialAllocate(cldMaterialAllocateDto);
            materialAppVo.setMeaterialOdd(cldMaterialAllocateDto.getMaterialOdd());
        } else if (materialAppVo.getApproveStatus() == 2) {
            materialAppVo.setRejectTime(DateUtils.getNowDate());
        }
        return cldMaterialAppMapper.updateCldMaterialApp(materialAppVo);
    }

    /**
     * 批量删除项目申请
     *
     * @param materialAppIds 需要删除的项目申请主键
     * @return 结果
     */
    @Override
    public int deleteCldMaterialAppByMaterialAppIds(Long[] materialAppIds) {
        return cldMaterialAppMapper.deleteCldMaterialAppByMaterialAppIds(materialAppIds);
    }

    /**
     * 删除项目申请信息
     *
     * @param materialAppId 项目申请主键
     * @return 结果
     */
    @Override
    public int deleteCldMaterialAppByMaterialAppId(Long materialAppId) {
        return cldMaterialAppMapper.deleteCldMaterialAppByMaterialAppId(materialAppId);
    }
}
